home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / newsgrp / group95b.txt / 000136_icon-group-sender _Thu Aug 24 09:40:09 1995.msg < prev    next >
Internet Message Format  |  1995-09-18  |  4KB

  1. Received: by cheltenham.cs.arizona.edu; Thu, 24 Aug 1995 12:33:27 MST
  2. To: icon-group@cs.arizona.edu
  3. Date: 24 Aug 1995 09:40:09 -0700
  4. From: yost@Yost.com (Dave Yost)
  5. Message-Id: <41ia19$rj@Yost.com>
  6. Organization: Dave Yost's house
  7. Sender: icon-group-request@cs.arizona.edu
  8. References: <41dvur$hvb@horus.infinet.com>, <qjg2irk72g.fsf@mpih16>
  9. Subject: Editing multiple files at once
  10. Errors-To: icon-group-errors@cs.arizona.edu
  11.  
  12. In article <qjg2irk72g.fsf@mpih16>,
  13. Jan Peter de Ruiter <janpeter@mpi.nl> wrote:
  14. >In article <41dvur$hvb@horus.infinet.com> btobin@infinet.com (Bruce S. Tobin) writes:
  15. >
  16. >    I'm looking for a better way to automate (or semi-automate) some heavy
  17. >   C++ editing I'm going to have to do soon, making lots of similar changes to
  18. >   lots of different files.  Is this a good use for Icon, or would I be 
  19. >   better off with sed, awk, or emacs lisp?  Thanks.
  20. >
  21. >Icon is just fine for this kind of job, but I believe you will save
  22. >yourself a lot of trouble and time if you take a look at "gema", a
  23. >text processing macro language that is just made for the kind of jobs
  24. >you mention. Take a look at it: http://www.ugcs.caltech.edu/gema/.
  25. >
  26. >Jan
  27.  
  28. One way to do what you want is to catenate all the text files together,
  29. edit them with whatever text editor you have handy, then burst the big
  30. file back into its constituent files.  So happens that the tool I wrote
  31. to do this is written in Icon.
  32.  
  33. Enjoy.
  34.  
  35. Dave
  36.  
  37. ======== bundle
  38. #!/bin/sh
  39.  
  40. # Author Dave@Yost.com
  41.  
  42. usage() {
  43.     echo 1>&2 "
  44. Usage: bundle file ...
  45.  
  46. Cats a set of text files together for editing.
  47. Use unbundle to re-extract them.
  48. "
  49.     exit 2
  50. }
  51.  
  52. case $# in
  53. 0) usage
  54. esac
  55.  
  56. for x
  57. do
  58.     if test -f $x -a -r $x
  59.     then
  60.     echo ' ========' $x
  61.     cat $x
  62.     fi
  63. done
  64. echo ' ========'
  65. ======== unbundle.icn
  66.  
  67. # Author Dave@Yost.com
  68.  
  69. procedure
  70. main (args)
  71.     local line
  72.     local pathname  # a/b/c
  73.     local dirname   # a/b
  74.     local filename  #     c
  75.     local tmpname   # a/b/c-UNBUNDLE-tmp
  76.     local file
  77.     local remove
  78.     local replace
  79.     local input
  80.     local verbose
  81.  
  82.     if *args ~= 1 then usage()
  83.  
  84.     verbose := 1
  85.     if not (input :=
  86.         if match ("-", args[1]) then &input else open (args[1], "r")) then {
  87.     write (&errout, "Can't open ", args[1])
  88.     exit (1)
  89.     }
  90.     while line := read(input) do {
  91.     line ? {
  92.         if tab(match (" ========")) then {
  93.         if \file then {
  94.             close (file)
  95.             remove := (if \verbose = 2 then
  96.                    "echo 1>&2 " || pathname || " unchanged;"
  97.                    else
  98.                    "") ||
  99.                   "rm -f " || tmpname
  100.             replace := (if \verbose = 1 then
  101.                     "echo 1>&2 " || pathname || ";"
  102.                 else
  103.                     "") ||
  104.                    "mv -f " || pathname || " " ||
  105.                        backupname (pathname) ||
  106.                    "; mv -f " || tmpname || " " || pathname
  107.             system ("if cmp 2>&1 > /dev/null " || tmpname || " " || pathname ||
  108.                 "; then " || remove || "; else " || replace ||
  109.                 "; fi")
  110.         }
  111.         if move(1) then {
  112.             pathname := tab(0)
  113.             filename := filepart (pathname)
  114.             if dirname := dirpart (pathname) then
  115.             system ("if test ! -d " || dirname ||
  116.                 "; then mkdir -p " || dirname || "; fi")
  117.             tmpname := pathname || "-UNBUNDLE-tmp"
  118.             if not (file := open (tmpname, "w")) then {
  119.             write (&errout, "Can't open temp file \"", tmpname, "\"")
  120.             exit (1)
  121.             }
  122.         } else
  123.             file := &null
  124.         } else {
  125.         write (file, line)
  126.         }
  127.     }
  128.     }
  129.     if \file then close (file)
  130.     return
  131. end
  132.  
  133. procedure
  134. backupname (pathname)
  135.     return dirpart (pathname) || "/," || filepart (pathname) |
  136.        "," || pathname
  137. end
  138.  
  139. procedure
  140. dirpart (pathname)
  141.     local ind
  142.     local dirname
  143.     local filename
  144.  
  145.     pathname ? {
  146.     (every ind := find ("/")) & return tab (ind)
  147.     }
  148. end
  149.  
  150.  
  151. procedure
  152. filepart (pathname)
  153.     local ind
  154.     local dirname
  155.     local filename
  156.  
  157.     pathname ? {
  158.     (every ind := find ("/")) & move (ind + 1)
  159.     filename := tab (0)
  160.     }
  161.     return filename
  162. end
  163.  
  164. procedure
  165. usage()
  166.     write (&errout, "\
  167. Usage: unbundle file\
  168. or:    unbundle -\
  169. \
  170. Unbundles a file made with the bundle command.\
  171. Each extracted file replaces (rather than overwrites)\
  172. any existing file by that name unless it is unchanged.\
  173. ")
  174.     exit (2)
  175. end
  176. ========
  177.